package com.antony.message1002;

import java.io.IOException;
import java.io.OutputStream;
import java.net.SocketException;
import java.util.ArrayList;

public class Message {
	private String name;
	private ArrayList<Message> arrMsg;
	private boolean start = true;
	private OutputStream out;
	private String msg;

	public Message(String name, OutputStream out, ArrayList<Message> arrMsg) {
		this.name = name;
		this.out = out;
		this.arrMsg = arrMsg;
	}

	public boolean getStart() {
		return start;
	}

	public String getName() {
		return name;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public void setMsgAll(String msg) {
		this.msg = msg;
		for (int i = 0; i < arrMsg.size(); i++) {
			arrMsg.get(i).setMsg(msg);
		}
		outPutAll();
	}

	public void exit() {
		start = false;
	}

	public void outPutAll() {
		for (int i = 0; i < arrMsg.size(); i++) {
			try {
				arrMsg.get(i).outPut();
			} catch (SocketException e1) {
				arrMsg.get(i).exit();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void outPut() throws IOException {
		out.write(msg.getBytes());
	}
}
